<template>
  <div class="section-create-or-edit">
    <el-form label-width="80px">
        <el-form-item label="课程名称" label-width="80px">
          <el-input disabled="disabled" v-model="this.courseName"></el-input>
        </el-form-item>
        <el-form-item label="章节名称">
          <el-input v-model="sectionForm.sectionName"></el-input>
        </el-form-item>
        <el-form-item label="章节描述">
          <el-input v-model="sectionForm.description" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="章节排序">
          <template>
            <el-input type="number" :min="0"
              v-model="sectionForm.orderNum">
              <template slot="append">数字控制排序，数字越大越靠后</template>
            </el-input>
          </template>
        </el-form-item>
    </el-form>
      <el-button @click="onCancel">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
  </div>
</template>

<script>
import { saveOrUpdateSection, getBySectionId } from '@/services/section'
export default {
  name: 'SectionCreateOrEdit',
  props: {
    courseName: {
      type: String
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    sectionId: {
      type: Number
    }
  },
  data () {
    return {
      sectionForm: {
        courseName: this.courseName,
        sectionName: '',
        description: '',
        orderNum: ''
      }
    }
  },
  created () {
    if (this.sectionId) {
      console.log(this.sectionId)
      this.loadSection()
    }
  },
  methods: {
    async onSubmit () {
      const { data } = await saveOrUpdateSection(this.sectionForm)
      console.log(data)
      if (data.code === '000000') {
        // 提示添加阶段成功并且清除表单数据，关闭对话框
        this.$message.success('已添加阶段')
        this.$emit('addSuccess')
        this.sectionForm = {}
      }
    },
    onCancel () {
      // 提示取消并且清除表单数据，关闭对话框
      this.$message.warning('已取消')
      this.$emit('cancelSuccess')
      this.sectionForm = {}
    },
    async loadSection () {
      console.log(this.sectionId)
      const data = await getBySectionId(this.sectionId)
      console.log(data)
    }
  }
}
</script>

<style lang="scss" scoped></style>
